package org.example.netty.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import org.example.netty.codec.MyDecoder;
import org.example.netty.codec.MyEncoder;

public class NettyServer {
    public static void main(String[] args) {
        //线程组-主要是监听客户端请求
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        //线程组-主要是处理具体业务
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        //启动类
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap
                //指定线程组
                .group(bossGroup, workerGroup)
                //指定 NIO 模式
                .channel(NioServerSocketChannel.class)
                //双向链表管理
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    protected void initChannel(NioSocketChannel ch) {
                        //责任链，指定自定义处理业务的 Handler
                     //   ch.pipeline().addLast(new NettyServerHandler());
                        //1.Netty内置拆包器
                        ch.pipeline().addLast(
                                new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,5,4)
                        );
//2.自定义解码器
                        ch.pipeline().addLast(new MyDecoder());
                        ch.pipeline().addLast(new MyEncoder());
//3.业务Handler
                        ch.pipeline().addLast(new ServerTestHandler());

                    }
                });
        //绑定端口号
        serverBootstrap.bind(8081);
    }
}

